3 use MediaWiki\Block\Restriction\PageRestriction
;
4 use MediaWiki\Block\Restriction\NamespaceRestriction
;
13 class ApiBlockTest
extends ApiTestCase
{
14 protected $mUser = null;
16 protected function setUp() {
18 $this->tablesUsed
= array_merge(
20 [ 'ipblocks', 'change_tag', 'change_tag_def', 'logging' ]
23 $this->mUser
= $this->getMutableTestUser()->getUser();
26 protected function getTokens() {
27 return $this->getTokenList( self
::$users['sysop'] );
31 * @param array $extraParams Extra API parameters to pass to doApiRequest
32 * @param User $blocker User to do the blocking, null to pick
35 private function doBlock( array $extraParams = [], User
$blocker = null ) {
36 if ( $blocker === null ) {
37 $blocker = self
::$users['sysop']->getUser();
40 $tokens = $this->getTokens();
42 $this->assertNotNull( $this->mUser
, 'Sanity check' );
43 $this->assertNotSame( 0, $this->mUser
->getId(), 'Sanity check' );
45 $this->assertArrayHasKey( 'blocktoken', $tokens, 'Sanity check' );
49 'user' => $this->mUser
->getName(),
50 'reason' => 'Some reason',
51 'token' => $tokens['blocktoken'],
53 if ( array_key_exists( 'userid', $extraParams ) ) {
54 // Make sure we don't have both user and userid
55 unset( $params['user'] );
57 $ret = $this->doApiRequest( array_merge( $params, $extraParams ), null,
60 $block = Block
::newFromTarget( $this->mUser
->getName() );
62 $this->assertTrue( !is_null( $block ), 'Block is valid' );
64 $this->assertSame( $this->mUser
->getName(), (string)$block->getTarget() );
65 $this->assertSame( 'Some reason', $block->mReason
);
73 public function testNormalBlock() {
80 public function testBlockById() {
81 $this->doBlock( [ 'userid' => $this->mUser
->getId() ] );
85 * A blocked user can't block
87 public function testBlockByBlockedUser() {
88 $this->setExpectedException( ApiUsageException
::class,
89 'You cannot block or unblock other users because you are yourself blocked.' );
91 $blocked = $this->getMutableTestUser( [ 'sysop' ] )->getUser();
93 'address' => $blocked->getName(),
94 'by' => self
::$users['sysop']->getUser()->getId(),
95 'reason' => 'Capriciousness',
96 'timestamp' => '19370101000000',
97 'expiry' => 'infinity',
101 $this->doBlock( [], $blocked );
104 public function testBlockOfNonexistentUser() {
105 $this->setExpectedException( ApiUsageException
::class,
106 'There is no user by the name "Nonexistent". Check your spelling.' );
108 $this->doBlock( [ 'user' => 'Nonexistent' ] );
111 public function testBlockOfNonexistentUserId() {
113 $this->setExpectedException( ApiUsageException
::class,
114 "There is no user with ID $id." );
116 $this->assertFalse( User
::whoIs( $id ), 'Sanity check' );
118 $this->doBlock( [ 'userid' => $id ] );
121 public function testBlockWithTag() {
122 ChangeTags
::defineTag( 'custom tag' );
124 $this->doBlock( [ 'tags' => 'custom tag' ] );
126 $dbw = wfGetDB( DB_MASTER
);
127 $this->assertSame( 1, (int)$dbw->selectField(
128 [ 'change_tag', 'logging', 'change_tag_def' ],
130 [ 'log_type' => 'block', 'ctd_name' => 'custom tag' ],
134 'change_tag' => [ 'JOIN', 'ct_log_id = log_id' ],
135 'change_tag_def' => [ 'JOIN', 'ctd_id = ct_tag_id' ],
140 public function testBlockWithProhibitedTag() {
141 $this->setExpectedException( ApiUsageException
::class,
142 'You do not have permission to apply change tags along with your changes.' );
144 ChangeTags
::defineTag( 'custom tag' );
146 $this->setMwGlobals( 'wgRevokePermissions',
147 [ 'user' => [ 'applychangetags' => true ] ] );
149 $this->doBlock( [ 'tags' => 'custom tag' ] );
152 public function testBlockWithHide() {
153 global $wgGroupPermissions;
154 $newPermissions = $wgGroupPermissions['sysop'];
155 $newPermissions['hideuser'] = true;
156 $this->mergeMwGlobalArrayValue( 'wgGroupPermissions',
157 [ 'sysop' => $newPermissions ] );
159 $res = $this->doBlock( [ 'hidename' => '' ] );
161 $dbw = wfGetDB( DB_MASTER
);
162 $this->assertSame( '1', $dbw->selectField(
165 [ 'ipb_id' => $res[0]['block']['id'] ],
170 public function testBlockWithProhibitedHide() {
171 $this->setExpectedException( ApiUsageException
::class,
172 "You don't have permission to hide user names from the block log." );
174 $this->doBlock( [ 'hidename' => '' ] );
177 public function testBlockWithEmailBlock() {
178 $res = $this->doBlock( [ 'noemail' => '' ] );
180 $dbw = wfGetDB( DB_MASTER
);
181 $this->assertSame( '1', $dbw->selectField(
184 [ 'ipb_id' => $res[0]['block']['id'] ],
189 public function testBlockWithProhibitedEmailBlock() {
190 $this->setExpectedException( ApiUsageException
::class,
191 "You don't have permission to block users from sending email through the wiki." );
193 $this->setMwGlobals( 'wgRevokePermissions',
194 [ 'sysop' => [ 'blockemail' => true ] ] );
196 $this->doBlock( [ 'noemail' => '' ] );
199 public function testBlockWithExpiry() {
200 $res = $this->doBlock( [ 'expiry' => '1 day' ] );
202 $dbw = wfGetDB( DB_MASTER
);
203 $expiry = $dbw->selectField(
206 [ 'ipb_id' => $res[0]['block']['id'] ],
210 // Allow flakiness up to one second
211 $this->assertLessThanOrEqual( 1,
212 abs( wfTimestamp( TS_UNIX
, $expiry ) - ( time() +
86400 ) ) );
215 public function testBlockWithInvalidExpiry() {
216 $this->setExpectedException( ApiUsageException
::class, "Expiry time invalid." );
218 $this->doBlock( [ 'expiry' => '' ] );
221 public function testBlockWithoutRestrictions() {
222 $this->setMwGlobals( [
223 'wgEnablePartialBlocks' => true,
228 $block = Block
::newFromTarget( $this->mUser
->getName() );
230 $this->assertTrue( $block->isSitewide() );
231 $this->assertCount( 0, $block->getRestrictions() );
234 public function testBlockWithRestrictions() {
235 $this->setMwGlobals( [
236 'wgEnablePartialBlocks' => true,
240 $page = $this->getExistingTestPage( $title );
241 $namespace = NS_TALK
;
245 'pagerestrictions' => $title,
246 'namespacerestrictions' => $namespace,
249 $block = Block
::newFromTarget( $this->mUser
->getName() );
251 $this->assertFalse( $block->isSitewide() );
252 $this->assertCount( 2, $block->getRestrictions() );
253 $this->assertInstanceOf( PageRestriction
::class, $block->getRestrictions()[0] );
254 $this->assertEquals( $title, $block->getRestrictions()[0]->getTitle()->getText() );
255 $this->assertInstanceOf( NamespaceRestriction
::class, $block->getRestrictions()[1] );
256 $this->assertEquals( $namespace, $block->getRestrictions()[1]->getValue() );
260 * @expectedException ApiUsageException
261 * @expectedExceptionMessage The "token" parameter must be set
263 public function testBlockingActionWithNoToken() {
267 'user' => $this->mUser
->getName(),
268 'reason' => 'Some reason',
272 self
::$users['sysop']->getUser()
277 * @expectedException ApiUsageException
278 * @expectedExceptionMessage Invalid value "127.0.0.1/64" for user parameter "user".
280 public function testBlockWithLargeRange() {
281 $tokens = $this->getTokens();
286 'user' => '127.0.0.1/64',
287 'reason' => 'Some reason',
288 'token' => $tokens['blocktoken'],
292 self
::$users['sysop']->getUser()
297 * @expectedException ApiUsageException
298 * @expectedExceptionMessage Too many values supplied for parameter "pagerestrictions". The
301 public function testBlockingToManyPageRestrictions() {
302 $this->setMwGlobals( [
303 'wgEnablePartialBlocks' => true,
306 $tokens = $this->getTokens();
311 'user' => $this->mUser
->getName(),
312 'reason' => 'Some reason',
314 'pagerestrictions' => 'One|Two|Three|Four|Five|Six|Seven|Eight|Nine|Ten|Eleven',
315 'token' => $tokens['blocktoken'],
319 self
::$users['sysop']->getUser()